package com.colourclash.fruitopia.views 
{
	import com.colourclash.fruitopia.controllers.FruitopiaGameController;
	import com.colourclash.fruitopia.models.FruitModel;
	import com.colourclash.fruitopia.particles.ParticleEffect;
	import com.colourclash.fruitopia.particles.StarParticles;
	import com.colourclash.fruitopia.resources.EmbeddedAssets;
	import com.colourclash.fruitopia.utils.BaseView;
	import com.colourclash.fruitopia.utils.Global;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.media.Sound;
	import flash.media.SoundTransform;

	/**
	 * This is the main game view
	 * 'Bound' to properties on the FruitopiaGameModel
	 * 
	 * @author Patrick Hammond
	 */
	public class FruitopiaGameView extends BaseView
	{
		
		private var _fruitContainer:DisplayObjectContainer; //container for fruit sprites
		
		private var _scoreView:ScoreView;
		
		private function getController():FruitopiaGameController
		{
			return controller as FruitopiaGameController;
		}
		

		//Constructor
		public function FruitopiaGameView(controller:FruitopiaGameController) 
		{
			drawView();
			
			super(controller);
		}
		
		private function drawView():void 
		{
			Global.getInstance().sizeToStage(addChild(new EmbeddedAssets.Background() as Bitmap));
			
			_fruitContainer = new Sprite() as DisplayObjectContainer;
			
			addChild(_fruitContainer);
			//listen for event indicating a Tween has completed
			_fruitContainer.addEventListener(Event.COMPLETE, animationsComplete, false, 0, true);
			
			Global.getInstance().sizeToStage(addChild(new EmbeddedAssets.Foreground() as Bitmap));
			
			var blenderView:BlenderView = new BlenderView();
			//listen for event indicating the blender has been clicked
			blenderView.addEventListener(Event.COMPLETE, blenderClickedHandler, false, 0, true);
			addChild(blenderView);
			
			_scoreView = new ScoreView();
			addChild(_scoreView);
		}
		
		//The fruits array in the model has changed
		//This method synchronises the view with the changes
		public function set fruits(value:Array):void 
		{
			for (var i:int = 0; i < 5; i++) 
			{
				for (var j:int = 0; j < 5; j++)
				{
					var fruitModel:FruitModel = value[i][j];

					var fruitView:FruitView = _fruitContainer.getChildByName("fruit" + String(fruitModel.id)) as FruitView;
					
					if (fruitView == null) //if a fruit doesn't exist, create one
					{
						getController().getNewFruit(fruitModel);
						fruitView = new FruitView(fruitModel);
						_fruitContainer.addChild(fruitView);
						
						//listen to events on the fruit for the user making a swap
						fruitView.addEventListener(MouseEvent.MOUSE_DOWN, fruitMouseDownHandler, false, 0, true);
						fruitView.addEventListener(MouseEvent.MOUSE_UP, fruitMouseUpHandler, false, 0, true);
					}
					else
					{
						if (fruitModel.isDestroyed) //if it has been destroyed, remove it
						{
							fruitView.removeEventListener(MouseEvent.MOUSE_DOWN, fruitMouseDownHandler);
							fruitView.removeEventListener(MouseEvent.MOUSE_UP, fruitMouseUpHandler);
							
							addChild(new ParticleEffect(StarParticles, new Point(fruitView.x, fruitView.y), .5));
							
							_fruitContainer.removeChild(fruitView);
							
							(new EmbeddedAssets.DropSound() as Sound).play(0, 0, new SoundTransform(.05));
							
							j--;
						}
						else //otherwise just update it's position
						{
							fruitView.updatePosition();
						}
					}
				}
			}
		}
		
		public function set score(value:int):void 
		{
			_scoreView.setScore(value);
		}
		
		private function fruitMouseDownHandler(e:MouseEvent):void 
		{
			if (TweenLite.getTweensOf(e.target).length == 0) //make sure animation isn't running
			{
				getController().selectFirstFruit((e.target as FruitView).model);
			}
		}
		
		private function fruitMouseUpHandler(e:MouseEvent):void 
		{
			if (TweenLite.getTweensOf(e.target).length == 0) //make sure animation isn't running
			{
				getController().selectSecondFruit((e.target as FruitView).model);
			}
		}
		
		private function animationsComplete(e:Event):void 
		{
			(new EmbeddedAssets.DropSound2() as Sound).play(0, 0, new SoundTransform(.05));
			
			//make sure no animations are running
			for (var i:int = 0; i < _fruitContainer.numChildren; i++) 
				if (TweenLite.getTweensOf(_fruitContainer.getChildAt(i)).length > 0)
					return;
			
			//inform the controller
			getController().turnComplete();
		}
		
				
		private function blenderClickedHandler(e:Event):void 
		{
			getController().destroyAllFruit();
		}
	}

}